
localStorageContent_index = 5;
timer = false;

date_current = "";


$(document).ready(function () {


    WdatePicker({
        onpicked: function () {

            date_current = $("#datepicker").val();

            localDatabase();

        }, el: 'datepicker'
    })

    date_current = getNowFormatDate();

    $("#datepicker").val(date_current);

    localDatabase();

    $.contextMenu({
        selector: '#notepad_div', //右键选择器
        callback: function (key, options) {//点击回调处理
            if (key == "jsonformatter") {
                jsonformatter();
            }
            else if (key == "formatcode") {
                formatcode();
            }
            else if (key == "shareurl") {
                shareurl();
            }
            else if (key == "fav") {
                fav();
            } else if (key == "favlist") {
                favlist();
            }
        },
        items: {//菜单列表配置
            "formatcode": { name: "格式化", icon: "" },
            "jsonformatter": { name: "Json格式化", icon: "" },
            "shareurl": { name: "分享链接", icon: "" },
            "sep1": "---------",
            "fav": { name: "添加到收藏", icon: "" },
            "favlist": { name: "收藏夹", icon: "" }
        }
    });
});


function jsonformatter() {

    require(["formatJson"], function () {

        try {

            var data = formatJson(document.getElementById("notepad_div").value);
            document.getElementById("notepad_div").value = data.trim();

        } catch (e) {

            alert(e.message);
        }

    });


}

function formatcode() {

    require(["https://elef.top/format/jsformat.js", "https://elef.top/format/htmlformat.js"], function () {

        var raw = $("#notepad_div").val();

        var js_source = raw.replace(/^\s+/, '');
        var tabsize = 1;
        var tabchar = '\t';

        var regEmptyTag = /(<([^\/][^>|^\/>].*)>)(\s*)?(<\/([^>]*)>)/g;
        var c = "";
        if (js_source && js_source.charAt(0) === '<') {

            c = style_html(js_source, tabsize, tabchar, 80);
        } else {

            c = js_beautify(js_source, tabsize, tabchar);
        }

        var data = c.replace(regEmptyTag, '$1$4');

        document.getElementById("notepad_div").value = data;
    });

}

function jsonview() {

    require(["https://cdn.bootcss.com/jquery-jsonview/1.2.3/jquery.jsonview.min.css", "https://cdn.bootcss.com/jquery-jsonview/1.2.3/jquery.jsonview.min.js"], function () {

        var raw_json = $("#notepad_div").val();
        var json = JSON.parse(raw_json);

        $("#objView").JSONView(json);

    });

}

function shareurl() {

    var raw_json = $("#notepad_div").val();

    $.post("https://elef.top/api.php", { type: "md", text: raw_json }, function (result) {

        var aString = "分享地址: <a href='" + result.url + "' target='_blank'>" + result.url + "</a>"

        $("#shareDiv").html(aString);

    });

}

let STORAGE_KEY = 'notepad-shoucang'; //存储键名称
let notepadStorage = {
    fetch: function () {//检索存储数据
        let records = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
        records.forEach(function (record, index) {
            record.id = index
        });
        notepadStorage.uid = records.length;
        return records
    },
    showAdd: function () {
        let records = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]');
        return (records.length > 0);
    },
    save: function (records) {//将数据保存到本地存储库
        localStorage.setItem(STORAGE_KEY, JSON.stringify(records))
    }
};

function fav() {
    var str = prompt("请填写标签", "");
    if (!str) {
        return;
    }

    var records = notepadStorage.fetch();

    id = records.push({//每次创建新记录，返回 id
        id: notepadStorage.uid++,
        title: '' + str,
        message: document.getElementById("notepad_div").value,
    });

    notepadStorage.save(records);
}
function favlist() {
    window.location.href = "sc/index.html";
}


